package com.smalldoor.thread.sync;

//死锁--避免死锁需要不去得到别人的
public class DeadLock {

    public static void main(String[] args) {
        Makeup firstGirl = new Makeup(0,"第一个女孩");
        Makeup secondGirl = new Makeup(1,"第二个女孩");

        firstGirl.start();
        secondGirl.start();
    }
}

//口红
class Lipstick{

}

//镜子
class Mirror{

}

class Makeup extends Thread {
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror = new Mirror();

    int choice;//选择
    String girlName;//使用化妆品的人名

    Makeup(int choice, String girlName) {
        this.choice = choice;
        this.girlName = girlName;
    }

    @Override
    public void run() {
        try {
            makeup();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private void makeup() throws InterruptedException {
        if (choice == 0) {
            synchronized (lipstick) {
                //获取口红的锁
                System.out.println(this.girlName + "获取口红锁");
                sleep(1000);
            }
            synchronized (mirror) {
                System.out.println(this.girlName + "获取镜子锁");
            }
        }else {
            synchronized (mirror) {
                //获取口红的锁
                System.out.println(this.girlName + "获取镜子锁");
                sleep(1000);
            }
            synchronized (lipstick) {
                System.out.println(this.girlName + "获取口红锁");
            }
        }
    }


}